#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "core/AnalyzeContext.h"
#include "ui/adddocumentdlg.h"
#include <QApplication>

using namespace TA;


static void setupTable(QTableWidget *table){
    table->verticalHeader()->hide();
    table->setSelectionBehavior(QAbstractItemView::SelectRows);
    table->setFrameShape(QFrame::NoFrame); //设置无边框
    table->setShowGrid(false); //设置不显示格子线
    table->setEditTriggers(QAbstractItemView::NoEditTriggers);
    table->setSelectionMode(QAbstractItemView::SingleSelection);
}

MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow) {
    coreService = new TA::CoreService(this);
    ui->setupUi(this);
    ui->menuBar->setNativeMenuBar(false);
    project = coreService->createProject(nullptr, "NoName");


    QTableWidget *table = ui->tbDocuments;
    table->setColumnCount(4);
    setupTable(table);

    table = ui->tbWords;
    table->setColumnCount(2);
    setupTable(table);
}

MainWindow::~MainWindow() {
    delete ui;
}


void MainWindow::on_actionOpen_triggered() {

}

void MainWindow::on_actionSave_triggered() {

}

void MainWindow::on_actionAddDocument_triggered() {
    AddDocumentDlg *addDocumentDlg = new AddDocumentDlg(this);

    connect(addDocumentDlg, &AddDocumentDlg::ok_clicked, this, &MainWindow::on_addDocument);

    AddDocumentDlg::showDlg(addDocumentDlg);
}

void MainWindow::on_addDocument(const QString &title, const QString &content) {
    Document *doc = coreService->appendDocument(project, title, content);

    if (doc == nullptr) {
        QMessageBox::critical(NULL, tr("critical"), tr("Document Title Exist"), QMessageBox::Ok, QMessageBox::Ok);
        return;
    }

    StatementRawPtrArray array = coreService->discriminateDoc(doc, ".\n\r");

    for (auto it = array.begin(); it != array.end(); ++it) {

        qDebug() << "statement:" << *(*it) << '\n';
    }

    AnalyzeContext *analyzeContext = new AnalyzeContext(this);
    analyzeContext->setDoc(doc);

    analyzeContext->appendStatement(array, 4);

    WordCache *cache = analyzeContext->getWordCache();

    for (auto it = cache->begin(); it != cache->end(); it++) {
        QString key = it.key();
        qDebug() << "key:" << key << '\n';
    }


    delete analyzeContext;

    refreshDocList();

}

void MainWindow::on_actionClose_triggered() {
    QApplication::exit();
}

void MainWindow::refreshDocList() {
    // QListWidget* list = ui->lstDocuments;

    QTableWidget *table = ui->tbDocuments;

    table->clear();

    DocumentRawPtrArray *array = project->getDocuments();

    int row = 0;
    for (auto it = array->begin(); it != array->end(); ++it, row++) {
        QTableWidgetItem *iText = new QTableWidgetItem();
        QTableWidgetItem *iUnCheck = new QTableWidgetItem();
        QTableWidgetItem *iCheck = new QTableWidgetItem();
        QTableWidgetItem *iCreateDate = new QTableWidgetItem();

        TA::Document *doc = *it;

        iText->setText(doc->getTitle());
        iUnCheck->setText(QString("%L1").arg(doc->getUnCheckedCount()));
        iCheck->setText(QString("%L1").arg(doc->getCheckedCount()));
        iCreateDate->setText(doc->getCreateDate().toString("yyyy-MM-dd"));

        int col = 0;

        table->insertRow(row);

        table->setItem(row, col++, iText);
        table->setItem(row, col++, iUnCheck);
        table->setItem(row, col++, iCheck);
        table->setItem(row, col++, iCreateDate);

    }

    QStringList header;
    header << tr("Title") << tr("UnChecked") << tr("Checked") << tr("Create Date");
    table->setHorizontalHeaderLabels(header);

    table->horizontalHeader()->resizeSection(1, 70);
    table->horizontalHeader()->resizeSection(2, 70);

}

void MainWindow::on_tbDocuments_itemSelectionChanged() {
    QTableWidget *table = ui->tbDocuments;

    QList<QTableWidgetItem *> items = table->selectedItems();

    if (items.size() > 0) {
        QTableWidgetItem *item = items.at(0);

        qDebug() << "select row:" << item->row() << " title:" << item->text();


        Document *doc = project->findDocument(item->text());

        if(this->doucment != doc)
            this->doucment = doc;

        if (doc != nullptr) {
            refreshDocument(doc);
        }

    } else {
        qDebug() << "select items size is 0!";
    }
}

void MainWindow::refreshDocument(TA::Document *doc) {

    ui->tfDocTitle->setText(doc->getTitle());
    ui->tfDocContent->setPlainText(doc->getContent());

    WordRawPtrArray* array = doc->getWords();

    QTableWidget *table = ui->tbWords;
    table->clear();
    int row = 0;
    for (auto it = array->begin(); it < array->end() ; ++it,row++) {

        Word* word = (*it);
        QTableWidgetItem *iWord = new QTableWidgetItem();
        iWord->setText(word->getWord());

        QTableWidgetItem *iWordCount = new QTableWidgetItem();
        iWordCount->setText(QString("%L1").arg(word->getPositions().size()));

        table->insertRow(row);

        table->setItem(row,0,iWord);
        table->setItem(row,1,iWordCount);

    }

    QStringList header;
    header << tr("Word")<< tr("Count") ;

    table->setHorizontalHeaderLabels(header);

}



void MainWindow::on_tbWords_itemSelectionChanged()
{
    QTableWidget *table = ui->tbWords;

    QList<QTableWidgetItem *> items = table->selectedItems();

    if (items.size() > 0) {
        QTableWidgetItem *item = items.at(0);

        qDebug() << "select row:" << item->row() << " title:" << item->text();



    } else {
        qDebug() << "select items size is 0!";
    }
}
